const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')

module.exports = {
  entry: {
    main: './src/index.js'
  },
  devtool: 'source-map-cheap-inline',
  mode: 'development',
  resolve: {
    extensions: ['.js'],
    mainFiles: ['index'],
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: {
          loader: 'file-loader'
        }
      },
      // {
      //   test: /\.m?js$/,
      //   exclude: /node_modules/,
      //   use: {
      //     loader: "babel-loader",
      //     options: {
      //       presets: [['@babel/preset-env',{
      //         useBuiltIns: 'usage'
      //       }]]
      //     }
      //   }
      // }
    ]
  },
  externals: ['BMapGL'],
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    chunkFilename: "[name].chunk.js",
    libraryTarget: "umd",
    library: 'library'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    }),
    new CleanWebpackPlugin(['dist'])
  ],
  devServer: {
    contentBase: './dist',
    hot: true,
    hotOnly: true,
    port: 8000
  },
  optimization: {
    splitChunks: {
      chunks: "all", // async-只对异步引入有效 all-同步异步都可 initial-同步代码
    },
  }
}
